(function(){
    'use strict';

    /**
     * 需要配合 laydate插件使用
     * @see https://www.layui.com/doc/modules/laydate.html
     */
    angular
    .module('smart.laydate', [])
    .directive('laydate', ['$compile', function($compile) {
        /**
         * 例子: <input laydate range="-" startDate="addData.startDay" endDate="addData.endDay" type="text" placeholder="開始日期-結束日期" autocomplete="off" />
         * @see https://www.layui.com/doc/modules/laydate.html
         *
         * @param datevalue 值, 字符串
         * @param datetype 类型：String，默认值：date (year, month, date, time, datetime)
         * @param range 如果设置 true，将默认采用 “ - ” 分割, 不填使用单个日期不使用范围
         * @param format 默认值：yyyy-MM-dd (yMdHms)
         * @param min 最小值
         * @param max 最大值
         * @param trigger 类型：String，默认值：focus，如果绑定的元素非输入框，则默认事件为：click
         * @param show 默认值：false, 如果设置: true，则控件默认显示在绑定元素的区域。通常用于外部事件调用控件
         * @param position 默认值：absolute, 其他: fixed static
         * @param showBottom 默认值：true, 如果设置 false，将不会显示控件的底部栏区域
         * @param btns 默认值：['clear', 'now', 'confirm']
         * @param calendar 默认值：false, 我们内置了一些我国通用的公历重要节日，通过设置 true 来开启。
         * @param mark 默认值：无, {}
         * @param ismarktoday 是否需要标注今天
         * @param ready function(date)
         * @param change function(value, date, enddate)
         * @param done function(value, date, enddate)
         */
        return {
            restrict: 'EA',
            replace: true,
            scope: {
                datevalue: "=",
                startdate: "=",
                enddate: "=",
                datetype: "@",
                range: "@",
                format: "@",
                min: "@",
                max: "@",
                trigger: "@",
                show: "@",
                position: "@",
                zindex: "@",
                showbottom: "@",
                btns: "@",
                calendar: "@",
                mark: "@",
                ismarktoday: "@",
                ready: "&",
                change: "&",
                done: "&"
            },
            compile: function(tElement) {
                return function($scope, element, $attrs) {
                    $scope.options = {};
                    $scope.options.elem = element[0];
                    $scope.options.type = $scope.datetype ? $scope.datetype : 'date';
                    $scope.options.format = $scope.format ? $scope.format : 'yyyy-MM-dd';
                    $scope.options.trigger = $scope.trigger ? $scope.trigger : 'click';
                    $scope.options.show = $scope.show ? $scope.show : false;
                    $scope.options.position = $scope.position ? $scope.position : 'absolute';
                    $scope.options.zindex = $scope.zindex ? $scope.zindex : 66666666;
                    $scope.options.showbottom = $scope.showbottom ? $scope.showbottom : true;
                    $scope.options.calendar = $scope.calendar ? $scope.calendar : false;
                    if ($scope.datevalue) {
                        $scope.options.value = $scope.datevalue;
                    }
                    if ($scope.min) {
                        $scope.options.min = $scope.min;
                    }
                    if ($scope.max) {
                        $scope.options.value = $scope.max;
                    }
                    if ($scope.range) {
                        // 如果设置了 startdate & enddate, 就设置初始值
                        $scope.options.range = $scope.range;
                        let label = ($scope.range == true ? '-' : $scope.range);
                        if ($scope.startdate && $scope.enddate) {
                            $scope.value = $scope.startdate + ' ' + label + ' ' + $scope.enddate
                            $scope.options.value = $scope.value;
                        }
                    }
                    if ($scope.btns) {
                        var btns = $scope.btns.split(",");
                        $scope.options.btns = []
                        for (var i = 0; i< btns.length; i++) {
                            if (btns[i]) {
                                $scope.options.btns.push($scope.options.btns[i].trim());
                            }
                        }
                    }
                    if ($scope.mark) {
                        $scope.options.mark = $scope.mark;
                    }
                    if ($scope.ismarktoday) {
                        let ismark = $scope.ismarktoday.toLocaleLowerCase();
                        $scope.ismarktoday = ismark === "false" ? false : $scope.ismarktoday;
                        if ($scope.ismarktoday) {
                            $scope.ismarktoday = ismark === "true" ? true : $scope.ismarktoday;
                        }
                    } else {
                        $scope.ismarktoday = true;
                    }
                    if ($scope.ismarktoday) {
                        if (!$scope.options.mark) {
                            $scope.options.mark = {}
                        }
                        let date = new Date();
                        let dateStr = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                        $scope.options.mark[dateStr] = typeof $scope.ismarktoday == 'boolean' ? date.getDate() : $scope.ismarktoday;
                    }
                    $scope.options.done = function(value, date, enddate) {
                        $scope.value = value;
                        $scope.datevalue = value;
                        $scope.startdate = date;
                        $scope.enddate = enddate;
                        $scope.change(value, date, enddate);
                        // $scope.$apply();
                    }
                    $scope.options.ready = $scope.ready;
                    $scope.options.change = $scope.change;

                    // 初始化 Laydate
                    $scope.laydate = laydate.render($scope.options);

                    /*
                    $scope.$watch("value", function(_new, _old) {
                        if (_new != _old) {

                        }
                    });
                    */
                }
            }
        };
    }]);
})();
